package com.ihrm.company.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import com.ihrm.company.service.CompanyService;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin//解决跨域问题
@RestController
@RequestMapping("/company")
public class CompanyController {

	@Autowired
	private CompanyService companyService;


	/**
	 * 保存企业
	 */
	@PostMapping
	public Result save(@RequestBody Company company){
		companyService.add(company);
		return Result.SUCCESS();
	}

	/**
	 * 根据id更新企业
	 */
	@PutMapping("/{companyId}")
	public Result update(@PathVariable(name = "companyId") String id, @RequestBody Company company){
		company.setId(id);
		companyService.update(company);
		return Result.SUCCESS();
	}


	/**
	 * 根据id删除企业
	 */
	@DeleteMapping("/{companyId}")
	public Result delete(@PathVariable(name = "companyId") String id){
		companyService.deleteById(id);
		return Result.SUCCESS();
	}

	/**
	 * 根据id查询企业
	 */
	@GetMapping("/{companyId}")
	public Result findById(@PathVariable(value = "companyId") String id) throws CommonException {
//		throw new CommonException(ResultCode.UNAUTHORISE);
		Company company = companyService.findById(id);
		return new Result(ResultCode.SUCCESS,company);
	}

	/**
	 * 查询所有企业列表
	 */
	@GetMapping
	public Result findAll(){
		int i = 1/0;
		return new Result(ResultCode.SUCCESS,companyService.findAll());
	}
}
